<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Paymenttypes extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('paymenttype');
	}
	public function ajax_paymenttype_get(){
		$paymenttypeEntity =  new CI_Payment_type_entity();
		$paymenttype = $paymenttypeEntity->getPaymentTypeByCode($_REQUEST['code']);
		echo json_encode($paymenttype);
	}
	public function ajax_paymenttype_list(){
		$paymenttypeEntity =  new CI_Payment_type_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$paymenttypeList = $paymenttypeEntity->getPaymentTypeListByLanguage($defaultLanguageId);
		$data['paymenttypeList'] = $paymenttypeList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/paymenttypes/index', $data);
	}
	public function ajax_paymenttype_save(){
		$paymenttypeNameList = $_REQUEST['paymenttype'];
		$paymenttypeNameDefault = '';
		foreach ($paymenttypeNameList as $key => $value) {
			if($value != null && $value != ""){
				$paymenttypeNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($paymenttypeNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_paymenttype_require');
		}else{
			$code = 0;
			foreach ($paymenttypeNameList as $key => $value) {
				$paymenttypeEntity =  new CI_Payment_type_entity();
				if($value != null && $value != ''){
					$paymenttypeEntity->setType($value);
				}else{
					$paymenttypeEntity->setType($paymenttypeNameDefault);
				}
				$paymenttypeEntity->setLanguageId($key);
				$paymenttypeEntity->setCode($code);
				$id = $paymenttypeEntity->save();
				if($code == 0){
					$code = $id;
					$paymenttypeEntity->setId($id);
					$paymenttypeEntity->setCode($code);
					$paymenttypeEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_paymenttype_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_paymenttype_update(){
		$paymenttypeNameList = $_REQUEST['paymenttype'];
		$paymenttypeIdList = $_REQUEST['paymenttype_id'];
		$paymenttypeCode = $_REQUEST['paymenttype_code'];
		$paymenttypeNameDefault = '';
		foreach ($paymenttypeNameList as $key => $value) {
			if($value != null && $value != ""){
				$paymenttypeNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($paymenttypeNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_paymenttype_require');
		}else{
			foreach ($paymenttypeNameList as $key => $value) {
				$paymenttypeEntity =  new CI_Payment_type_entity();
				if($value != null && $value != ''){
					$paymenttypeEntity->setType($value);
				}else{
					$paymenttypeEntity->setType($paymenttypeNameDefault);
				}
				$paymenttypeEntity->setLanguageId($key);
				$paymenttypeEntity->setCode($paymenttypeCode);
				$paymenttypeEntity->setId($paymenttypeIdList[$key]);
				$paymenttypeEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_paymenttype_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_paymenttype_delete(){
		$result = array();
		$paymenttypeEntity =  new CI_Payment_type_entity();
		$result['result'] = $paymenttypeEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_paymenttype_delete_success');
		}else{
			$result['message'] = lang('escort_paymenttype_delete_error');
		}
		echo json_encode($result);
	}
}
?>